package com.xu.junit;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasValue;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.xu.common.example.model.User;

public class JUnitStudy1_base_AssertThat {
	private static final List<User> user = new ArrayList<User>();
	private static final User test1 = new User();
	private static final User test2 = new User();
	private static final Map<String, User> userMap = new HashMap<String, User>();
	
	static {
		test1.setUsername("123");
		test2.setUsername("abc");
		user.add(test1);
		user.add(test2);
		userMap.put(test1.getUsername(), test1);
		userMap.put(test2.getUsername(), test2);
	}

	@Test
	public void test() {
		/*
		 * assertThat语法 assertThat(T actual, Matcher<T> matcher);
		 * assertThat(String reason, T actual, Matcher<T> matcher);
		 * 入参actual为实际值，入参matcher为期待值的匹配符
		 */

		// 测试变量是否大于指定值
		assertThat(100, greaterThan(50));
		// 测试变量是否小于指定值
		assertThat(1, lessThan(100));
		// 测试变量是否大于等于指定值
		assertThat(50, greaterThanOrEqualTo(50));
		// 测试变量是否小于等于指定值
		assertThat(1, lessThanOrEqualTo(100));

		// 测试所有条件必须成立
		assertThat(51, allOf(greaterThan(50), lessThan(100)));
		// 测试只要有一个条件成立
		assertThat(51, anyOf(greaterThanOrEqualTo(50), lessThanOrEqualTo(100)));
		// 测试无论什么条件成立(还没明白这个到底是什么意思)
		assertThat(1, anything());
		// 测试变量值等于指定值
		assertThat(100, is(100));
		// 测试变量不等于指定值
		assertThat(1, not(50));

		/** 字符串 **/
		String url = "http://www.taobao.com";
		// 测试变量是否包含指定字符
		assertThat(url, containsString("taobao"));
		// 测试变量是否已指定字符串开头
		assertThat(url, startsWith("http://"));
		// 测试变量是否以指定字符串结尾
		assertThat(url, endsWith(".com"));
		// 测试变量是否等于指定字符串
		assertThat(url, equalTo("http://www.taobao.com"));
		// 测试变量再忽略大小写的情况下是否等于指定字符串
		assertThat(url, equalToIgnoringCase("http://www.taobao.com"));
		// 测试变量再忽略头尾任意空格的情况下是否等于指定字符串
		assertThat(url, equalToIgnoringWhiteSpace("http://www.taobao.com"));

		// 测试集合中是否还有指定元素
		assertThat(user, hasItem(test1));
		assertThat(user, hasItem(test2));

		// 测试map中是否还有指定键值对
		assertThat(userMap, hasEntry(test1.getUsername(), test1));
		// 测试map中是否还有指定键
		assertThat(userMap, hasKey(test2.getUsername()));
		// 测试map中是否还有指定值
		assertThat(userMap, hasValue(test2));
	}

}
